#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mem(x) memset(x,0,sizeof(x))
#define endl "\n"
#define printYes cout << "Yes\n"
#define printYES cout << "YES\n"
#define printNo cout << "No\n"
#define printNO cout << "NO\n"
#define lowbit(x) ((x)&(-(x)))
#define pb push_back
#define mkp make_pair
#define pii pair<int,int>
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define rep(i,j,k) for (int i=(j);i<=(k);i++)
#define per(i,j,k) for (int i=(j);i>=(k);i--)
#define pcnt(x) __builtin_popcount(x)
mt19937 rnd(time(0));
template<class T>void chkmin(T&x,T y){x=min(x,y);}
template<class T>void chkmax(T&x,T y){x=max(x,y);}

const ll inf=1000000000000000000; 
//const ll inf=1000000000;
//const ll mod=998244353;
//const ll mod=1000000007;

const int N=105,M=1e6+5;
int n,k,tot,m; 
map<vector<int>,int>q;
vector<int>st,p[M];
int a[N];
ll dp[M],g[M];
void dfs(int x,int y)
{
	if (x>k)
	{
		q[st]=++tot;p[tot]=st;
		return;
	}
	rep(i,y,n/k)
	{
		st.pb(i);
		m+=i;
		dfs(x+1,i);
		st.pop_back();
		m-=i;
	}
}
void solve(vector<int>a,int len,ll x,int p)
{
	if (p+1<len) return;
	if (len==0)
	{
		chkmin(dp[q[a]],x);
		return;
	}
	solve(a,len,x,p-1);
	if (p==k-1||a[p]!=a[p+1])
	{
		a[p]++;
		solve(a,len-1,x,p-1);
	}
}
void work(int len,ll x)
{
	rep(i,1,tot) g[i]=dp[i],dp[i]=inf;
	rep(i,1,tot)
	{
		if (g[i]==inf) continue;
		solve(p[i],len,g[i],k-1);
	}
	rep(i,1,tot)
	{
		if (dp[i]==inf) continue;
		int num=0;
		for (int j:p[i]) if (j!=0&&j!=n/k) num++;
		dp[i]+=x*num;
	}
}

int main()
{
	freopen("diyiti.in","r",stdin);
	freopen("diyiti.out","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n >> k;
	dfs(1,0);
	rep(i,1,n) cin >> a[i];
	sort(a+1,a+n+1);
	rep(i,1,tot) dp[i]=inf;
	dp[1]=0;
	a[n+1]=a[n];
	rep(i,1,n)
	{
		int j=i;
		while (j<n&&a[j+1]==a[j]) j++;
		work(j-i+1,a[j+1]-a[j]);
		i=j;
	}
	ll ans=inf;
	rep(i,1,tot) chkmin(ans,dp[i]);
	if (ans==inf) cout << -1;
	else cout << ans;
	return 0;
}
